<?php

namespace Biz\service\activity\lottery;

use Doraemon\model\AdmUserType;
use Doraemon\model\DeviceBlackWhite;
use Doraemon\model\Phone;
use Exception;

class LotteryActivityRules
{

    private $uid;
    private $did;
    private $userInfo;
    private $userQuality;
    private $userNick;
    private $config;
    private string $userIdentityMark;
    private array $err = [];
    const RULE_B1 = 1;
    const RULE_PHONE = 1 << 1;
    const RULE_BATCH_TAG = 1 << 2;
    const RULE_DEVICE = 1 << 3;
    const RULE_COMPANY = 1 << 4;
    private static ?LotteryActivityRules $instance = null;

    public static function getInstance($uid = 0, $did = '', $config = 0): ?LotteryActivityRules
    {
        if (!self::$instance) {
            self::$instance = new self();
            self::$instance->uid = $uid;
            self::$instance->did = $did;
            self::$instance->config = $config;
            self::$instance->init();
        }
        return self::$instance;
    }

    public function error(): array
    {
        return $this->err;
    }

    public function exec(): bool
    {
        $pass = true;
        try {
            if ($this->config & self::RULE_B1) $this->filterB1();
            if ($this->config & self::RULE_PHONE) $this->filterPhone();
            if ($this->config & self::RULE_BATCH_TAG) $this->filterBatchTag();
            if ($this->config & self::RULE_DEVICE) $this->filterDevice();
            if ($this->config & self::RULE_COMPANY) $this->filterCompany();
        } catch (Exception $e) {
            $pass = false;
            $this->err[] = $e->getMessage();
        }
        return $pass;
    }

    protected function init()
    {
        $aMemberQualityInfo = AdmUserType::getByUid($this->uid) ?: [];
        $this->userQuality = $aMemberQualityInfo['quality'] ?? '';
        $this->userIdentityMark = trim($aMemberQualityInfo['identity_mark'] ?? '');
    }

    /**
     * @throws Exception
     */
    protected function filterB1()
    {
        if ($this->userQuality == AdmUserType::B1)  throw new Exception('B级用户');
    }

    /**
     * @throws Exception
     */
    protected function filterBatchTag()
    {
        if (strpos($this->userIdentityMark, "批量") !== false) throw new Exception('批量标识的账号');
//        if (strpos($this->userIdentityMark, "测试账号") !== false) throw new Exception('测试账号');
//        if (strpos($this->userIdentityMark, "疑似商业用户") !== false) throw new Exception('疑似商业用户');
//        if (strpos($this->userIdentityMark, "机器人账号") !== false) throw new Exception('机器人账号');
//        if (strpos($this->userIdentityMark, "积分反垃圾") !== false) throw new Exception('积分反垃圾');
    }

    /**
     * @throws Exception
     */
    protected function filterDevice()
    {
        if (DeviceBlackWhite::selectByWhere([
            'device_id' => $this->did,
            'data_type' => DeviceBlackWhite::DEVICE_BLACK_TYPE,
            'data_status' =>DeviceBlackWhite::STATUS_NORMAL,
        ])) throw new Exception('被拉黑的设备ID:' . $this->did);
    }

    /**
     * @throws Exception
     */
    protected function filterCompany()
    {
        if (in_array($this->uid, \Biz\tools\helper\Common::getHhzWorkerUids())) throw new Exception('公司用户白单');
    }

    /**
     * @throws Exception
     */
    protected function filterPhone()
    {
        $aMemberPhone = Phone::getByUid($this->uid);
        if (null == $aMemberPhone) {
            throw new \Exception('没绑定手机号');
        }
        if (0 === strpos($aMemberPhone['hidden_phone'], '142')) {
            throw new \Exception('142测试用户');
        }
    }

    /**
     * @throws Exception
     */
    protected function filterNick()
    {
        if (strpos($this->userNick, "住友_") !== false) throw new Exception('没有修改过昵称的账号');
    }


}